# 字典类型
from datetime import datetime

from sqlalchemy import func
from sqlalchemy.orm import aliased

from comm.sqlalchemyConfig import db


class funsModel(db.Model):
    """
    菜单表
    """
    __tablename__ = 'tx_funs'
    id = db.Column(db.String(100), primary_key=True, autoincrement=False,doc='id')
    code = db.Column(db.String(100), nullable=False,unique=True,doc='code')
    name = db.Column(db.String(125), nullable=False,doc='菜单名称')
    router = db.Column(db.String(500), nullable=False,doc='菜单路由')
    component = db.Column(db.String(500), nullable=False,doc='菜单组件')
    parent_code = db.Column(db.String(500), nullable=True,default=None,doc='上级菜单',comment="使用code作为是上级菜单")
    status = db.Column(db.Boolean, nullable=True,default=1,doc='状态')
    sort = db.Column(db.Integer, nullable=True,default=1000,doc='排序')
    create_time = db.Column(db.DateTime, default=datetime.now, nullable=True)
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now, nullable=True)
    @classmethod
    def getAll(cls):
        # 获取所有使用中的模块
        dataList = []
        data = cls.query.filter(cls.status==1,func.coalesce(cls.parent_code,'')=='').all()
        for itm in data:
            itm_z = {}
            itm_z['id'] = itm.id
            itm_z['code'] = itm.code
            itm_z['name'] = itm.name
            dataList.append(itm_z)
        return dataList

    @classmethod
    def getTreeAll(cls,serializeFilterList):
        # 获取所有模块且为属性接口
        dataList = []
        aliasedCls = aliased(cls)
        filedList = [
            cls.id,
            cls.code,
            cls.name,
            cls.router,
            cls.component,
            cls.parent_code,
            cls.status,
            cls.sort,
            cls.create_time,
            cls.update_time,
            cls.name.label('parent_name')
        ]
        modelDataObj1 = (db.session.query(*filedList).filter(*serializeFilterList,func.coalesce(cls.parent_code,'')=='').
                        join(aliasedCls,
                          cls.parent_code == aliasedCls.code,
                          isouter=True).all())
        for itm1 in modelDataObj1:
            itm1_z = {}
            itm1_z['id'] = itm1.id
            itm1_z['code'] = itm1.code
            itm1_z['name'] = itm1.name
            itm1_z['router'] = itm1.router
            itm1_z['component'] = itm1.component
            itm1_z['parent_code'] = itm1.parent_code
            itm1_z['status'] = itm1.status
            itm1_z['sort'] = itm1.sort
            itm1_z['parent_name'] = itm1.parent_name
            # itm1_z['hasChildren'] = False
            itm1_z['children'] = []
            modelDataObj2 = (db.session.query(*filedList).filter(*serializeFilterList,cls.parent_code==itm1.code).
                             join(aliasedCls,
                                  cls.parent_code == aliasedCls.code,
                                  isouter=True).all())

            if len(modelDataObj2)>0:
                # itm1_z['hasChildren'] = True
                for itm2 in modelDataObj2:
                    itm2_z = {}
                    itm2_z['id'] = itm2.id
                    itm2_z['code'] = itm2.code
                    itm2_z['name'] = itm2.name
                    itm2_z['router'] = itm2.router
                    itm2_z['component'] = itm2.component
                    itm2_z['parent_code'] = itm2.parent_code
                    itm2_z['status'] = itm2.status
                    itm2_z['sort'] = itm2.sort
                    itm2_z['parent_name'] = itm2.parent_name
                    itm2_z['children'] = []
                    itm1_z['children'].append(itm2_z)
            dataList.append(itm1_z)
        return dataList